/*
 *  Licensed to PECATS under one or more contributor license agreements below:
 *  1. http://www.mozilla.org/MPL/MPL-1.1.html
 *  2. http://www.gnu.org/licenses/gpl-2.0.html
 *  3. http://www.gnu.org/licenses/lgpl-2.1.html
*/

/* **********************************************************
*  Servlet for Handling Customer Login
*  Name: LoginManagerServlet.java
*  Author: www.pecats.com
*  Date: Sep 15, 2008
************************************************************/

package com.pecats.server.core;

import com.pecats.server.core.CustomerAccountManager;
import com.pecats.server.core.Customer;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import java.io.IOException;

/**
 * Servlet used to manage login, logout request from the customer
 */

public class LoginManagerServlet extends HttpServlet 
{

    RequestDispatcher dispatcher;

    /*
     * Method used to handle incoming http request.
     * @param request HttpServletRequest object from the incoming request
     * @param response HttpServletResponse object which forms the response
     * @exception ServletException
     * @exception IOException
    */

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
            throws ServletException, IOException 
	{

	    	// Get the session
	      boolean create = true;         
	      HttpSession session = request.getSession(create);

		String action = (String) request.getParameter("action");
		System.out.println(action);
		if (action.equalsIgnoreCase("login"))
		{
			session.setAttribute("Email", request.getParameter("Email"));
			try
			{
		            CustomerAccountManager customManager = new CustomerAccountManager();
				if (request.getParameter("Password").equals(customManager.checkCustomerLogin(request.getParameter("Email"))))
				{
					request.setAttribute("loginmessage", "Login Successfull");
					request.setAttribute("Email", request.getParameter("Email"));
					dispatcher = getServletContext().getRequestDispatcher("/BrowseJSP.jsp");		
					dispatcher.forward(request, response);
				}
				else
				{
					request.setAttribute("loginmessage", "Either your username or password is incorrect, please try again" );
					dispatcher = getServletContext().getRequestDispatcher("/LoginJSP.jsp");		
					dispatcher.forward(request, response);
				}
		
	            } 
			catch (Exception ex) 
			{
            		throw new ServletException(ex);
	        	}
		}
		else if (action.equalsIgnoreCase("logout"))
		{
			session.removeAttribute("email");
			if (session != null) 
			{
				session.invalidate();
			}
			RequestDispatcher rd = request.getRequestDispatcher("/LoginJSP.jsp");
			rd.forward(request, response);
		}
	}
}